package com.ahmedmisbah.andgenplus.utils.json.mapper;

import java.io.IOException;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.ahmedmisbah.andgenplus.utils.json.exceptions.Json2PojoMapperException;
import com.ahmedmisbah.andgenplus.utils.json.mapper.test.User;

public class Json2ObjectMapper<T> {

	private static ObjectMapper mapper = new ObjectMapper();

	public T mapJson2Object(String jsonString, Class classObject)
			throws Json2PojoMapperException {

		T object = null;

		try {
			object = (T) mapper.readValue(jsonString, classObject);
		} catch (JsonParseException e) {
			throw new Json2PojoMapperException(e);
		} catch (JsonMappingException e) {
			throw new Json2PojoMapperException(e);
		} catch (IOException e) {
			throw new Json2PojoMapperException(e);
		}

		return object;

	}

	public static void main(String args[]) throws Exception {
		Json2ObjectMapper<User> jsonMapper = new Json2ObjectMapper<User>();
		User user = jsonMapper.mapJson2Object("{"
				+ "\"name\" : { \"first\" : \"Joe\", \"last\" : \"Sixpack\" },"
				+

				"\"gender\" : \"MALE\"," + " \"verified\" : false,"
				+ "\"userImage\" : \"Rm9vYmFyIQ==\"" + "}", User.class);

		System.out.println(user.getName().getFirst());
	}

}
